package coolapk.cj.demo.controller;

import coolapk.cj.demo.domain.Good;
import coolapk.cj.demo.domain.JSONResult;
import coolapk.cj.demo.domain.Person;
import coolapk.cj.demo.domain.PersonTest;
import coolapk.cj.demo.service.good.GoodService;
import coolapk.cj.demo.service.person.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: demo
 * @description: 物品
 * @author: SDH
 * @create: 2019-04-16 16:45
 */
@RestController
@ResponseBody
public class GoodController {
	@Autowired
	GoodService goodService;
	@Autowired
	PersonService personService;

	/**
	 * 新增物品
	 *
	 * @param name
	 * @param num
	 * @return
	 */
	@PostMapping({"/insertGood"})
	public JSONResult insertGood(String name, int num) {
		Good good = new Good();
		good.setName(name);
		good.setNum(num);
		good.setFlag(0);
		int result = goodService.insert(good);
		if (result > 0) {
			return JSONResult.build(200, "ok", null);
		} else {
			return JSONResult.build(500, "error", null);
		}
	}

	/**
	 * 获得所有抽奖物品
	 *
	 * @return
	 */
	@GetMapping({"/getAllGoods"})
	public JSONResult getAllGoods() {
		List<Good> goodList = goodService.getAllGoods();
		return JSONResult.build(200, "ok", goodList);
	}

	/**
	 * 设置当前抽奖项
	 *
	 * @param id
	 * @return
	 */
	@PostMapping({"/setGood"})
	public JSONResult setGoods(int id) {
		List<Good> goodList = goodService.getAllGoods();
		for (Good d : goodList) {
			if (d.getFlag() == 1) {
				d.setFlag(0);
				goodService.updateByPrimaryKey(d);
			}
		}
		Good good = goodService.selectByPrimaryKey(id);
		good.setFlag(1);
		int result = goodService.updateByPrimaryKey(good);
		if (result > 0) {
			return JSONResult.build(200, "ok", null);
		} else {
			return JSONResult.build(500, "error", null);
		}
	}

	/**
	 * 获得当前抽奖项
	 *
	 * @return
	 */
	@GetMapping({"/getGood"})
	public JSONResult getGood() {
		List<Good> goodList = goodService.getAllGoods();
		Good good = null;
		for (Good d : goodList) {
			if (d.getFlag() == 1) {
				good = d;
			}
		}
		if (good != null) {
			return JSONResult.build(200, "ok", good);
		} else {
			return JSONResult.build(500, "当前无抽奖物品", null);
		}
	}

	/**
	 * 抽奖.获得幸运群友 根据当前抽奖项和报名情况
	 *
	 * @return
	 */
	@GetMapping({"/randomPerson"})
	public JSONResult randomPerson() {
		List<Good> goodList = goodService.getAllGoods();
		Good good = null;
		for (Good d : goodList) {
			if (d.getFlag() == 1) {
				good = d;
			}
		}
		if (good != null) {
			List<Person> personList = personService.getAllPersonByGoodId(good.getId());
			if (personList.size() != 0) {
				List<Person> personList1 = new LinkedList<>();
				for(int i =0;i<good.getNum();i++) {
					Person person = randomPerson(personList);
					personList1.add(person);
					personList.remove(person);
					personService.deleteByPrimaryKey(person.getId());
				}
//				good.setFlag(0);
//				goodService.updateByPrimaryKey(good);
				return JSONResult.build(200, "ok", personList1);
			} else {
				return JSONResult.build(500, "无人报名", null);
			}
		} else {
			return JSONResult.build(500, "无抽奖项", null);
		}
	}

	private Person randomPerson(List<Person> personList) {
		int[] randoms = new int[10];
		for (int i = 0; i < 10; i++) {
			int random = (int) Math.floor(Math.random() * personList.size());
			randoms[i] = random;
		}
		int randomIndex = (int) Math.floor(Math.random() * 10);
		return personList.get(randoms[randomIndex]);
	}

	/**
	 * 测试抽奖概率
	 *
	 * @return
	 */
	@GetMapping({"/test"})
	public JSONResult Test() {
		List<Good> goodList = goodService.getAllGoods();
		Good good = null;
		for (Good d : goodList) {
			if (d.getFlag() == 1) {
				good = d;
			}
		}
		if (good != null) {
			List<Person> personList = personService.getAllPersonByGoodId(good.getId());
			List<PersonTest> personTests = new ArrayList<>();
			for (Person p : personList) {
				personTests.add(new PersonTest(p, 0));
			}
			for (int i = 0; i < 10000; i++) {
				Person p = randomPerson(personList);
				for (PersonTest p1 : personTests
				) {
					if (p.getId() == p1.getPerson().getId()) {
						p1.setCount(p1.getCount() + 1);
					}
				}
			}
			return JSONResult.build(200, "ok", personTests);
		}
		return null;
	}
}
